//
//  FourViewController.m
//  MainDemo
//
//  Created by FM on 2017/1/9.
//  Copyright © 2017年 FM. All rights reserved.
//

#import "FourViewController.h"
#import "UIView+Extension.h"
#import "Define.h"
#import "BaseTableView.h"
#import "CustomSegmentView.h"
#import "ItemsTableViewCell.h"
#import "ItemsViewController.h"

@interface FourViewController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>
{
    UIView *tableHeadView;
}
@property (nonatomic ,strong)NSArray *dataArray;
@property (nonatomic ,strong) BaseTableView *myTableView;
@property (nonatomic, strong) CustomSegmentView * RCSegView;
@property (nonatomic, strong) ItemsTableViewCell * containerCell;
@property (nonatomic, assign) BOOL canScroll;
@end

@implementation FourViewController
-(id)init{
    self=[super init];
    if (self) {
        self.title=@"tableView的嵌套";
        _dataArray=@[@"图片选择",@"按钮样式",@"弹窗"];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor=[UIColor purpleColor];
    
    [self.view addSubview:self.myTableView];
    [self creatTableHeadView];
    _myTableView.tableHeaderView=tableHeadView;
    self.canScroll = YES;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeScrollStatus) name:@"leaveTop" object:nil];
}

#pragma mark - Notification

- (void)changeScrollStatus {
    self.canScroll = YES;
    self.containerCell.objectCanScroll = NO;
}
-(void)creatTableHeadView{
    
    CGFloat sceneImgViewW=(WIDTH-kPercenX_scale(15)*2-5)/2;
    CGFloat sceneImgViewX=kPercenX_scale(15)+sceneImgViewW+5;
    CGFloat sceneImgViewH=(kPercenX_scale(215)-5)/2;
    
    tableHeadView=[[UIView alloc]init];
    tableHeadView.userInteractionEnabled=YES;
    tableHeadView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:tableHeadView];
    
    UIImageView  *sceneImgView;
    for (int i=0; i<6; i++) {
        sceneImgView=[[UIImageView alloc]init];
        //        sceneImgView.image=PlaceholderIMG(sceneImgViewW, kPercenX_scale(220));
        [tableHeadView addSubview:sceneImgView];
        sceneImgView.tag=10+i;
        //        sceneImgView.contentMode=UIViewContentModeScaleToFill;
        sceneImgView.userInteractionEnabled=YES;
//        [sceneImgView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickToScene:)]];
        switch (i) {
            case 0:
                sceneImgView.frame=CGRectMake(kPercenX_scale(15), kPercenX_scale(15), sceneImgViewW, kPercenX_scale(215));
                break;
            case 1:
                sceneImgView.frame=CGRectMake(sceneImgViewX, kPercenX_scale(15), sceneImgViewW, sceneImgViewH);
                break;
            case 2:
                sceneImgView.frame=CGRectMake(sceneImgViewX, kPercenX_scale(15)+5+sceneImgViewH, sceneImgViewW, sceneImgViewH);
                break;
            case 3:
                sceneImgView.frame=CGRectMake(kPercenX_scale(15), kPercenX_scale(15)+kPercenX_scale(215)+5, sceneImgViewW, sceneImgViewH);
                break;
            case 4:
                sceneImgView.frame=CGRectMake(kPercenX_scale(15), kPercenX_scale(15)+kPercenX_scale(215)+10+sceneImgViewH, sceneImgViewW, sceneImgViewH);
                break;
            case 5:
                sceneImgView.frame=CGRectMake(sceneImgViewX, kPercenX_scale(15)+kPercenX_scale(215)+5, sceneImgViewW, kPercenX_scale(215));
                break;
                
            default:
                break;
        }
        
    }
//    for (HomeProjectModel *model in qianJiaModel.projectRooms) {
//        if ([model.name isEqualToString:@"新唐演艺"]) {
//            UIImageView *imgView1=(UIImageView *)[self.view viewWithTag:10];
//
//            //            [imgView1 sd_setImageWithURL:[NSURL URLWithString:model.image] placeholderImage:PlaceholderIMG(imgView1.width, imgView1.height)];
//        }else if ([model.name isEqualToString:@"醇酒人生"]) {
//            UIImageView *imgView2=(UIImageView *)[self.view viewWithTag:11];
//            //            [imgView2 sd_setImageWithURL:[NSURL URLWithString:model.image] placeholderImage:PlaceholderIMG(imgView2.width, imgView2.height)];
//
//        }else if ([model.name isEqualToString:@"藤铁故事"]) {
//            UIImageView *imgView3=(UIImageView *)[self.view viewWithTag:12];
//            //            [imgView3 sd_setImageWithURL:[NSURL URLWithString:model.image] placeholderImage:PlaceholderIMG(imgView3.height, imgView3.width)];
//
//        }else if ([model.name isEqualToString:@"摆渡暹罗"]) {
//            UIImageView *imgView4=(UIImageView *)[self.view viewWithTag:13];
//            //            [imgView4 sd_setImageWithURL:[NSURL URLWithString:model.image] placeholderImage:PlaceholderIMG(imgView4.width, imgView4.height)];
//
//
//        }else if ([model.name isEqualToString:@"竹园茶艺"]) {
//            UIImageView *imgView5=(UIImageView *)[self.view viewWithTag:14];
//            //            [imgView5 sd_setImageWithURL:[NSURL URLWithString:model.image] placeholderImage:PlaceholderIMG(imgView5.width, imgView5.height)];
//
//        }else{//if ([model.name isEqualToString:@"印度风情"])
//            UIImageView *imgView6=(UIImageView *)[self.view viewWithTag:15];
//
//            //            [imgView6 sd_setImageWithURL:[NSURL URLWithString:model.image] placeholderImage:PlaceholderIMG(imgView6.width, imgView6.height)];
//        }
//
//    }
    UIImageView *moreImg=[[UIImageView alloc]init];
    moreImg.frame=CGRectMake(kPercenX_scale(15), CGRectGetMaxY(sceneImgView.frame)+5, WIDTH-kPercenX_scale(15)*2, kPercenX_scale(45));
    moreImg.image=Image_name(@"查看更多");
    moreImg.userInteractionEnabled=YES;
//    [moreImg addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickToMore)]];
    [tableHeadView addSubview:moreImg];
    
    UIView *lineView=[[UIView alloc]init];
    lineView.backgroundColor=Color_e5e5e5;
    lineView.frame=CGRectMake(0, CGRectGetMaxY(moreImg.frame)+kPercenX_scale(15), WIDTH, kPercenX_scale(10));
    [tableHeadView addSubview:lineView];
    
    UILabel *titleLab=[[UILabel alloc]init];
    titleLab.text=@"情景推荐";
    titleLab.frame=CGRectMake(kPercenX_scale(15), CGRectGetMaxY(lineView.frame)+kPercenX_scale(15), WIDTH, [Helper heightWithfont:18]);
    titleLab.textColor=Color_333333;
    titleLab.font=Font_Regular(18);
    [tableHeadView addSubview:titleLab];
    
    UILabel *subtitleLab=[[UILabel alloc]init];
    subtitleLab.text=@"Recommend ";
    subtitleLab.frame=CGRectMake(kPercenX_scale(16), CGRectGetMaxY(titleLab.frame), WIDTH/2, [Helper heightWithfont:12]);
    subtitleLab.font=Font_Regular(12);
    [tableHeadView addSubview:subtitleLab];
    subtitleLab.textColor=UIColorFromRGB(0X999999);
    tableHeadView.frame=CGRectMake(0, 0, WIDTH, CGRectGetMaxY(subtitleLab.frame)+kPercenX_scale(0));
    
}
////  单元格下滑线  紧贴左边
//-(void)viewDidLayoutSubviews {
//    
//    if ([_myTableView respondsToSelector:@selector(setSeparatorInset:)]) {
//        [_myTableView setSeparatorInset:UIEdgeInsetsZero];
//        
//    }
//    if ([_myTableView respondsToSelector:@selector(setLayoutMargins:)])  {
//        [_myTableView setLayoutMargins:UIEdgeInsetsZero];
//    }
//}
//-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
//        [cell setSeparatorInset:UIEdgeInsetsZero];
//    }
//
//    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
//        [cell setLayoutMargins:UIEdgeInsetsZero];
//    }
//}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == self.myTableView) {
        
        CGFloat offset = StatusBarAndNavBarHeight;
        
        CGFloat bottomCellOffset = [self.myTableView rectForSection:0].origin.y - offset;

        bottomCellOffset = floorf(bottomCellOffset);
   
        if (scrollView.contentOffset.y >= bottomCellOffset) {
        
            scrollView.contentOffset = CGPointMake(0, bottomCellOffset);
            if (self.canScroll) {
                
                self.canScroll = NO;
                self.containerCell.objectCanScroll = YES;
            }
        }else{
            
            //子视图没到顶部
            if (!self.canScroll) {
           
                scrollView.contentOffset = CGPointMake(0, bottomCellOffset);

            }
        }
    }
}
//-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;{

    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;{
    
    
    static NSString *cellID=@"cellID";

    
    ItemsTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellID];
    
    if (!cell) {
        cell=[[ItemsTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        //单元格点击后的选择状态
        
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
    }
    self.containerCell=cell;
    
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return  self.view.frame.size.height - StatusBarAndNavBarHeight - TabbarHeight;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    switch (indexPath.row) {
        case 0:
        {

            
        }
            break;
        case 1:
        {
          
            
        }
            break;
        case 2:
        {
            
        }
            break;
            
        default:
            break;
    }


}
-(void)sendPhotoImages:(NSArray *)images;{

    NSLog(@"%@",images);
}
-(BaseTableView *)myTableView{
    if (!_myTableView) {
        _myTableView=[[BaseTableView alloc]initWithFrame:CGRectMake(0, 0,WIDTH , HEIGHT-49) style:UITableViewStylePlain];
        _myTableView.estimatedRowHeight=30;
        _myTableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        _myTableView.showsVerticalScrollIndicator=NO;
        _myTableView.delegate=self;
        _myTableView.dataSource=self;
    }
    return _myTableView;
}
-(UIView *)setPageViewControllers
{
    if (!_RCSegView) {
        ItemsViewController *sceneVC=[[ItemsViewController alloc]init];
   
        ItemsViewController *sceneVC1=[[ItemsViewController alloc]init];

        NSArray *controllers=@[sceneVC,sceneVC1];
         NSArray *titleArray =@[@"page1",@"page2"];
        
        CustomSegmentView * rcs=[[CustomSegmentView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-64) controllers:controllers titleArray:titleArray ParentController:self lineWidth:WIDTH/5 lineHeight:3. andSelect:0];
        rcs.backgroundColor=[UIColor whiteColor];
//        rcs.delegate=self;
        _RCSegView = rcs;
    }
    return _RCSegView;
}
@end
